package me.pinion.model.book;

import freemarker.template.TemplateModel;
import me.pinion.kit.DateKit;
import me.pinion.model.User;
import me.pinion.model.XModel;
import me.pinion.model.annotation.TableBind;
import me.pinion.service.BookService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@TableBind(table = "book")
public class Book extends XModel<Book>{

    BookService bookService;




    public static Book dao = new Book();
    private static Map<Object, Book> BOOK_CACHE = new HashMap<Object, Book>();

    private String bookname;
    private String description;

    @Override
    public Book findById(Object id) {
//        Book book = BOOK_CACHE.get(id);
//        if (book == null){
          Book  book = super.findById(id);
//            if (book != null){
//                BOOK_CACHE.put(id, book);
//            }
//        }
        return book;
    }

    public Book findByBookname(String bookname){
        return findFirst("SELECT * FROM book AS b WHERE b.bookname=?", bookname);
    }

    public String getBookname(){
        if (bookname == null){
            bookname = getStr("bookname");
        }
        return this.getStr("bookname");
    }

    public User getAuthor(){ return User.dao.findById(this.getInt("author_id")); }

    public String getDescription(){
        if(this.description == null){
            this.description = getStr("description");
        }
        if (this.description == null){
            this.description = "暂时没有介绍";
        }
        return this.description;
    }

    public String getPseudonym(){ return getStr("pseudonym"); }

    public String getCreateDateString(){ return DateKit.toStr(this.getTimestamp("datetime"), "YY-MM-dd");}

    public List<Chapter> getChapters(){
        return Chapter.dao.find("SELECT " +
                "ch.id, " +
                "ch.title, " +
                "ch.book_id, " +
                "ch.last_update FROM chapter AS ch " +
                "WHERE ch.book_id=? AND ch.enabled=true ",
                this.getInt("id"));
    }

    public Chapter getChapterByTitle(String title){
        return Chapter.dao.findFirst("SELECT " +
                "ch.id, " +
                "ch.title, " +
                "ch.book_id, " +
                "ch.last_update FROM chapter AS ch " +
                "WHERE ch.book_id=? AND ch.title=? and ch.enabled=true ",
                this.getInt("id"), title);
    }

    public Chapter getRecentChapter(){
        return Chapter.dao.findFirst("SELECT " +
                "ch.id, " +
                "ch.title, " +
                "ch.book_id, " +
                "ch.last_update " +
                "FROM chapter AS ch " +
                "WHERE ch.book_id=? AND ch.enabled=true " +
                "ORDER BY last_update DESC ", this.getInt("id"));

    }

    public List<User> getRecentVisitors(){
        return User.dao.paginate(1, 10, "SELECT \n" +
                "\tu.*, max(v.datetime)\n",
                "FROM\n" +
                "    user AS u,\n" +
                "\tx_visit AS v\n" +
                "\t\n" +
                "WHERE\n" +
                "    u.id = v.user\n" +
                "\tAND v.target_type=?\n" +
                "\tAND v.target_id=?\n" +
                "GROUP BY u.id\n" +
                "ORDER BY max(v.datetime) DESC",
                this.getModelId(),this.getId()).getList();
    }

    public List<Book> listByOwner(User user){ return dao.find("SELECT * FROM book AS b WHERE b.author_id=? AND b.enabled=true ", user.getId());}

    public boolean isAuthor(User user){ return user != null && user.equals(this.getAuthor()); }
}
